#include <iostream>
#include <pthread.h>
#include <string>
#include <unistd.h>
#include "thread.hpp"
#include "lockguard.hpp"

int tickets = 1000;
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;

void thread_run(void* args)
{
    while (true)
    {
        lock_guard lg(&mtx);

        if (tickets > 0)
        {
            usleep(2000);
            std::cout << (char*)args << " get a ticket, last: " << tickets-- << std::endl;
        }
        else
        {
            break;
        }
    }
}

int main()
{
    thread t1(thread_run, (void*)"hello world_1");
    thread t2(thread_run, (void*)"hello world_2");
    thread t3(thread_run, (void*)"hello world_3");
    thread t4(thread_run, (void*)"hello world_4");

    t1.join();
    t2.join();
    t3.join();
    t4.join();

    return 0;
}
